package com.dom.domweb.dao.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dom.common.constants.ConstantError;
import com.dom.common.enums.PubStatEnum;
import com.dom.common.model.vo.basic.*;
import com.dom.domweb.common.RspModel;
import com.dom.domweb.dao.entity.TMeasureUnitTbl;
import com.dom.domweb.dao.entity.TSuppInfo;
import com.dom.domweb.dao.mapper.TSuppInfoMapper;
import com.dom.domweb.dao.service.ITSuppInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * B_供货商信息表 服务实现类
 * </p>
 *
 * @author xllei
 * @since 2018-11-17
 */
@Service
public class TSuppInfoServiceImpl extends ServiceImpl<TSuppInfoMapper, TSuppInfo> implements ITSuppInfoService {
    @Override
    public List<TSuppInfo> getSuppList(int memCode){
        Wrapper<TSuppInfo> wrapper = new EntityWrapper<>();

        return selectList(wrapper);
    }

    @Override
    public RspModel supplierInfoDel(String id) {
        Wrapper<TSuppInfo> wrapper = new EntityWrapper<TSuppInfo>();
        wrapper.eq("id",id);
//        TSuppInfo tblInfo = new TSuppInfo();
//        tblInfo.setStat(PubStatEnum.STAT_STOP.getCode());
        int count = this.baseMapper.delete(wrapper);
        RspModel model = new RspModel("RSP000000","供货商信息删除成功！");
        if (count != 1){
            model = new RspModel(ConstantError.TRADEERROR);
        }
        return model;
    }

    @Override
    public RspModel editSupplierInfo(B010R request) {
        Wrapper<TSuppInfo> wrapper = new EntityWrapper<TSuppInfo>();
        wrapper.eq("id",request.getId());
//        TSuppInfo tbl = this.baseMapper.selectById(request.getId());
//        if (tbl.getStat() != PubStatEnum.STAT_UNACTIVATED.getCode()
//                &&  !request.getUnitCode().equals(Integer.toString(tbl.getUnitCode())) ){
//            return new RspModel(ConstantError.NOT_CODE_ERROR);
//        }
        TSuppInfo tblInfo = new TSuppInfo();
        tblInfo.setSuppCode(request.getSuppCode() == null ? null : Integer.valueOf(request.getSuppCode()));
        tblInfo.setSuppName(request.getSuppName());
        tblInfo.setAbbrName(request.getAbbrName());
        tblInfo.setOrganLicense(request.getOrganLicense());
        tblInfo.setAddress(request.getAddress());
        tblInfo.setLegalName(request.getLegalName());
        tblInfo.setCertNo(request.getCertNo());
        tblInfo.setCertType(StringUtils.isEmpty(request.getCertType()) ? null : Integer.valueOf(request.getCertType()));
        tblInfo.setSuppType(StringUtils.isEmpty(request.getSuppType()) ? null : Integer.valueOf(request.getSuppType()));
        tblInfo.setBusiMain(request.getBusiMain());
        tblInfo.setContName(request.getLinkMan());
        tblInfo.setContPhone(request.getLinkPhone());
        tblInfo.setContEmail(request.getLinkEmail());
//        tblInfo.setStat(StringUtils.isEmpty(request.getStat()) ? PubStatEnum.STAT_UNACTIVATED.getCode() : Integer.valueOf(request.getStat()));
        int count = this.baseMapper.update(tblInfo,wrapper);
        RspModel model = new RspModel("RSP000000","供应商信息修改成功！");
        if (count != 1){
            model = new RspModel(ConstantError.TRADEERROR);
        }
        return model;
    }

    @Override
    public RspModel insertSupplierInfo(B010R request) {
        Wrapper<TSuppInfo> wrapper = new EntityWrapper<TSuppInfo>();
        wrapper.eq("id",request.getId());
        List<TSuppInfo> list = this.baseMapper.selectList(wrapper);
        if (list != null && list.size() != 0){
            return new RspModel("RSP000032","供货商已存在！");
        }
        TSuppInfo tblInfo = new TSuppInfo();
        tblInfo.setMemCode(0);
        tblInfo.setSuppCode(request.getSuppCode() == null ? null : Integer.valueOf(request.getSuppCode()));
        tblInfo.setSuppName(request.getSuppName());
        tblInfo.setAbbrName(request.getAbbrName());
        tblInfo.setOrganLicense(request.getOrganLicense());
        tblInfo.setAddress(request.getAddress());
        tblInfo.setLegalName(request.getLegalName());
        tblInfo.setCertNo(request.getCertNo());
        tblInfo.setCertType(StringUtils.isEmpty(request.getCertType()) ? null : Integer.valueOf(request.getCertType()));
        tblInfo.setSuppType(StringUtils.isEmpty(request.getSuppType()) ? null : Integer.valueOf(request.getSuppType()));
        tblInfo.setBusiMain(request.getBusiMain());
        tblInfo.setContName(request.getLinkMan());
        tblInfo.setContPhone(request.getLinkPhone());
        tblInfo.setContEmail(request.getLinkEmail());
        tblInfo.setVersion(0);
        int count = this.baseMapper.insert(tblInfo);
        RspModel model = new RspModel("RSP000000","供货商信息新增成功！");
        if (count != 1){
            model = new RspModel(ConstantError.TRADEERROR);
        }
        return model;
    }

    @Override
    public B013A getSupplierInfoByCode(String id) {
        TSuppInfo tbl =  this.baseMapper.selectById(id);
        B013A result = new B013A();
        result.setId(tbl.getId().toString());
        result.setSuppCode(tbl.getSuppCode() == null ? null : tbl.getSuppCode().toString());
        result.setSuppName(tbl.getSuppName());
        result.setAbbrName(tbl.getAbbrName());
        result.setOrganLicense(tbl.getOrganLicense());
        result.setAddress(tbl.getAddress());
        result.setLegalName(tbl.getLegalName());
        result.setCertNo(tbl.getCertNo());
        result.setCertType(tbl.getCertType() == null ? null : tbl.getCertType().toString());
        result.setSuppType(tbl.getSuppType() == null ? null : tbl.getSuppType().toString());
        result.setBusiMain(tbl.getBusiMain());
        result.setLinkMan(tbl.getContName());
        result.setLinkPhone(tbl.getContPhone());
        result.setLinkEmail(tbl.getContEmail());
        return result;
    }

    @Override
    public B019A getAllSupplierInfo() {
        B019A b = new B019A();
        Wrapper<TSuppInfo> wrapper = new EntityWrapper();
        List<TSuppInfo> list = this.baseMapper.selectList(wrapper);
        List<SuppInfo> result = infoToTbl(list);
        b.setSuppInfoData(result);
        return b;
    }

    private List<SuppInfo> infoToTbl(List<TSuppInfo> list) {
        List<SuppInfo> result = new ArrayList<>();
        for (TSuppInfo tblInfo:  list) {
            SuppInfo info = new SuppInfo();
            info.setId(tblInfo.getId());
            info.setMemCode(tblInfo.getMemCode());
            info.setSuppCode(tblInfo.getSuppCode());
            info.setSuppName(tblInfo.getSuppName());
            info.setAbbrName(tblInfo.getAbbrName());
//            info.setOrganLicense(tblInfo.getOrganLicense());
            info.setAddress(tblInfo.getAddress());
//            info.setLegalName(tblInfo.getLegalName());
//            info.setCertNo(tblInfo.getCertNo());
//            info.setCertType(tblInfo.getCertType() == null ? null : tblInfo.getCertType().toString());
//            info.setSuppType(tblInfo.getSuppType() == null ? null : tblInfo.getSuppType().toString());
            info.setBusiMain(tblInfo.getBusiMain());
            info.setContName(tblInfo.getContName());
            info.setContPhone(tblInfo.getContPhone());
//            info.setLinkEmail(tblInfo.getContEmail());
            result.add(info);
        }
        return result;
    }

    @Override
    public B019A getSupplierInfoList(B019R request) {
        Wrapper<TSuppInfo> wrapper = new EntityWrapper();
        wrapper.like("abbr_name",request.getAbbrname());
//        wrapper.notIn("stat", PubStatEnum.STAT_STOP.getCode());
        Page<TSuppInfo> page = new Page<>();
        page.setCurrent(request.getPn());
        page.setSize(request.getPs());
        page = this.selectPage(page,wrapper);
        B019A result = new B019A();
        result.setTotalNum((int)page.getTotal());
        List<SuppInfo> list = infoToTbl(page.getRecords());
        result.setSuppInfoData(list);
        return result;
    }

    @Override
    public TSuppInfo getSuppInfo(int memCode, Integer suppCode) {
        Wrapper<TSuppInfo> wrapper = new EntityWrapper<>();
        wrapper.eq("mem_code", memCode)
                .and().eq("supp_code", suppCode)
                .last("limit 1");
        return this.selectOne(wrapper);
    }
}
